import Vue from 'vue'
import VueRouter from 'vue-router'

import routes from './routes'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes
})

function isAuthenticated () {
  const token = localStorage.getItem('token')

  return !!token
}

const whiteList = ['Login', 'Home', 'Register', 'Mine', '/']

router.beforeEach((to, from, next) => {
  console.log(to.name, from.name)
  if (!whiteList.includes(to.name) && !isAuthenticated()) next({ name: 'Login' })
  else next()
})

export default router
